package com.kzj.fun.container;

import com.kzj.common.log.Logger;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

public class ScalablePanel extends JPanel implements MouseListener, MouseMotionListener {
    static int cursorSize=32;
    private Component lastComponent;
    private Point lastPoint;

    @Override
    public void mouseClicked(MouseEvent e) {

    }

    @Override
    public void mousePressed(MouseEvent e) {
        this.lastPoint=e.getPoint();
    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {
        lastComponent=KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();
        if(!this.requestFocusInWindow()){
            Logger.warn(String.format("%s 焦点获取失败 %s",this.getName(),"(mouseEntered)"));
        }
    }

    @Override
    public void mouseExited(MouseEvent e) {
        if(!lastComponent.requestFocusInWindow()){
            Logger.warn(String.format("%s 焦点归还-> %s 失败 %s",this.getName(),lastComponent.getCursor(),"(mouseExited)"));
        }
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        Point self = this.getLocation();
        switch (getCursor().getType()) {
            case Cursor.MOVE_CURSOR: {
                int xdif = e.getX() - lastPoint.x;
                int ydif = e.getY() - lastPoint.y;
                setLocation(self.x + xdif, self.y + ydif);
            }
            break;
            case Cursor.N_RESIZE_CURSOR: {
                int ydif = e.getY() - lastPoint.y;
                setLocation(self.x, self.y + ydif);
                setSize(getWidth(), getHeight() + ydif);
            }
            break;
            case Cursor.S_RESIZE_CURSOR: {
                int ydif = e.getY() - lastPoint.y;
                setSize(getWidth(), getHeight() + ydif);
            }
            break;
            case Cursor.W_RESIZE_CURSOR: {
                int xdif = e.getX() - lastPoint.x;
                setLocation(self.x + xdif, self.y);
                setSize(getWidth() + xdif, getHeight());
            }
            break;
            case Cursor.E_RESIZE_CURSOR: {
                int xdif = e.getX() - lastPoint.x;
                setSize(getWidth() + xdif, getHeight());
            }
            break;
            case Cursor.NW_RESIZE_CURSOR: {
                int xdif = e.getX() - lastPoint.x;
                int ydif = e.getY() - lastPoint.y;
                setLocation(self.x + xdif, self.y + ydif);
                setSize(getWidth() + xdif, getHeight() + ydif);
            }
            break;
            case Cursor.NE_RESIZE_CURSOR: {
                int xdif = e.getX() - lastPoint.x;
                int ydif = e.getY() - lastPoint.y;
                setLocation(self.x, self.y + ydif);
                setSize(getWidth() + xdif, getHeight() + ydif);
            }
            break;
            case Cursor.SW_RESIZE_CURSOR: {
                int xdif = e.getX() - lastPoint.x;
                int ydif = e.getY() - lastPoint.y;
                setLocation(self.x + xdif, self.y);
                setSize(getWidth() + xdif, getHeight() + ydif);
            }
            break;
            case Cursor.SE_RESIZE_CURSOR: {
                int xdif = e.getX() - lastPoint.x;
                int ydif = e.getY() - lastPoint.y;
                setSize(getWidth() + xdif, getHeight() + ydif);
            }
        }
        lastPoint=e.getPoint();
        this.repaint();
    }

    @Override
    public void mouseMoved(MouseEvent e) {
        Point self = this.getLocation();
        Point point=e.getPoint();
        if(point.x<=self.x-cursorSize/2||point.x>=self.x+this.getWidth()+cursorSize/2||point.y<=self.y-cursorSize/2||point.y>=self.y+this.getHeight()+cursorSize/2){
            this.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
        }
        else if(point.x>=self.x+cursorSize/2&&point.x<=self.x+this.getWidth()-cursorSize/2&&point.y>=self.y+cursorSize/2&&point.y<=self.y+this.getHeight()-cursorSize/2){
            this.setCursor(new Cursor(Cursor.MOVE_CURSOR));
        }
        else if(point.x>self.x+cursorSize/2&&point.x<self.x+this.getWidth()-cursorSize/2){
            if(point.y>self.y-cursorSize/2&&point.y<self.y+cursorSize/2){
                this.setCursor(new Cursor(Cursor.N_RESIZE_CURSOR));
            }else if(point.y>self.y+this.getHeight()-cursorSize/2&&point.y<self.y+this.getHeight()+cursorSize/2){
                this.setCursor(new Cursor(Cursor.S_RESIZE_CURSOR));
            }
        }else if(point.y>self.y+cursorSize/2&&point.y<self.y+this.getHeight()-cursorSize/2){
            if(point.x>self.x-cursorSize/2&&point.x<self.x+cursorSize/2){
                this.setCursor(new Cursor(Cursor.W_RESIZE_CURSOR));
            }else if(point.x>self.x+this.getWidth()-cursorSize/2&&point.x<self.x+this.getWidth()+cursorSize/2){
                this.setCursor(new Cursor(Cursor.E_RESIZE_CURSOR));
            }
        }else if(point.y>=self.y-cursorSize/2&&point.y<=self.y+cursorSize/2){
            if(point.x>=self.x-cursorSize/2&&point.x<=self.x+cursorSize/2){
                this.setCursor(new Cursor(Cursor.NW_RESIZE_CURSOR));
            }else if(point.x>=self.x+this.getWidth()-cursorSize/2&&point.x<=self.x+this.getWidth()+cursorSize/2){
                this.setCursor(new Cursor(Cursor.NE_RESIZE_CURSOR));
            }
        }else if(point.y>=self.y+this.getHeight()-cursorSize/2&&point.y<=self.y+this.getHeight()+cursorSize/2) {
            if (point.x >= self.x - cursorSize / 2 && point.x <= self.x + cursorSize / 2) {
                this.setCursor(new Cursor(Cursor.SW_RESIZE_CURSOR));
            } else if (point.x >= self.x + this.getWidth() - cursorSize / 2 && point.x <= self.x + this.getWidth() + cursorSize / 2) {
                this.setCursor(new Cursor(Cursor.SE_RESIZE_CURSOR));
            }
        }
        this.repaint();
    }
}
